package com.zjw.zy

import androidx.appcompat.app.AlertDialog
import com.zjw.zy.base.BaseActivity
import com.zjw.zy.constant.AppConstant
import com.zjw.zy.constant.HttpConstant
import com.zjw.zy.utils.LogUtils
import okhttp3.*
import java.io.IOException
import java.util.*
import kotlin.system.exitProcess

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/9/29 13:28
 * @description：
 */
class CrashActivity : BaseActivity() {

    private var mDialog: AlertDialog? = null

    companion object {
        const val CRASH_MESSAGE_KEY = "CRASH_MESSAGE_KEY"
    }


    override fun onBindListener() {
    }

    override fun onBindValue() {

    }

    override fun onFindView() {

    }

    override fun onResume() {
        super.onResume()

        val crashMessage = intent.getStringExtra(CRASH_MESSAGE_KEY)

        mDialog = AlertDialog.Builder(this)
            .setTitle("出现程序错误啦！洋洋，截图发给我看看~嘿嘿")
            .setPositiveButton(
                "关闭程序"
            ) { _, _ ->
                mDialog?.hide()
                finish()
                exitProcess(0)
            }
            .setMessage(crashMessage)
            .create()

        //发送云端
        val client = OkHttpClient.Builder().build()
        val call = client.newCall(Request.Builder().apply {
            this.url(
                HttpConstant.HEART_SERVICE_BASE_URL + "cloud/crash/msg?time=${Date()}&crash=${crashMessage}}"
            )
        }.build())

        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                LogUtils.e("error!!!!!", e.message)
            }

            override fun onResponse(call: Call, response: Response) {
                LogUtils.e("error!!!!!", "")
            }

        })

        mDialog?.show()
    }

    override fun onLayoutId(): Int {
        return R.layout.crash_activity
    }
}